<?php
//phpcms 模板渲染便捷方法
class TbfPhpcms{
    //模板渲染便捷方法
    static function GetParentCatidList($CAT){
        if (empty($CAT['arrparentid'])){
            return array($CAT['catid']);
        }
        $output =  explode(',',$CAT['arrparentid']);
        $output[] = $CAT['catid'];
        return $output;
    }

    /*
    获取点击量
    详情页面使用方法 <?php echo TbfPhpcms::GetHits('c-'.$CAT['modelid'].'-'.$rs['id']);?>
    */
    static function GetHits($hitsid){
        $viewRow = TbfDatabase::getOneById('v9_hits',$hitsid,'hitsid');
        if (empty($viewRow['views'])){
            return 0;
        }
        return $viewRow['views'];
    }
    //增加点击量,返回增加后的总点击量,并发访问时,点击量会少
    static function AddHits($hitsid){
        //$hitsid = 'c-'.$modelid.'-'.$dataid;
        $r = TbfDatabase::getOneById('v9_hits',$hitsid,'hitsid');
        if(!$r){
            try{
                TbfDatabase::insert('v9_hits',array('hitsid'=>$hitsid,'views'=>1));
            }catch(Exception $e){}
            return 1;
        }
        $views = $r['views'] + 1;
        $yesterdayviews = (date('Ymd', $r['updatetime']) == date('Ymd', strtotime('-1 day'))) ? $r['dayviews'] : $r['yesterdayviews'];
        $dayviews = (date('Ymd', $r['updatetime']) == date('Ymd', SYS_TIME)) ? ($r['dayviews'] + 1) : 1;
        $weekviews = (date('YW', $r['updatetime']) == date('YW', SYS_TIME)) ? ($r['weekviews'] + 1) : 1;
        $monthviews = (date('Ym', $r['updatetime']) == date('Ym', SYS_TIME)) ? ($r['monthviews'] + 1) : 1;
        $sql = array(
            'views'=>$views,
            'yesterdayviews'=>$yesterdayviews,
            'dayviews'=>$dayviews,
            'weekviews'=>$weekviews,
            'monthviews'=>$monthviews,
            'updatetime'=>SYS_TIME,
            'hitsid'=>$hitsid);
        TbfDatabase::updateById('v9_hits',$sql, 'hitsid');
        return $sql;
    }
    //重组推荐位数组
    public  function arr($data){
         $arr=array();
         foreach ($data as $v) {
             $arr[]=$v;
         }
         return $arr;
     }
     //通过id获取栏目信息
     function getcat($id){
        $arr=array($id);
        $data=TbfDatabase::query("select * from `v9_category` where `catid`=?",$arr);
        return $data;

     }
}
